package com.beilang.hotline.telreply.manager.impl;

import com.beilang.base.util.StringUtil;
import com.beilang.base.util.CodeConstant;
import com.beilang.base.codelookup.manager.CodeLookupMgr;
import com.beilang.hotline.telreply.dao.TelreplyDAO;
import com.beilang.hotline.telreply.manager.TelreplyMgr;
import com.beilang.hotline.telreply.model.TelreplyModel;
import com.beilang.hotline.user.manager.UserMgr;
import com.beilang.hotline.user.model.UserModel;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class TelreplyMgrImpl implements TelreplyMgr {

    private TelreplyDAO telreplyDAO;

    private UserMgr userMgr;

    private CodeLookupMgr codeLookupMgr;

    public void setCodeLookupMgr(CodeLookupMgr codeLookupMgr) {
        this.codeLookupMgr = codeLookupMgr;
    }

    public void setUserMgr(UserMgr userMgr) {
        this.userMgr = userMgr;
    }

    public void setTelreplyDAO(TelreplyDAO telreplyDAO) {
        this.telreplyDAO = telreplyDAO;
    }

    public TelreplyModel createTelreplyModel(TelreplyModel telreplyModel) {
        telreplyModel = telreplyDAO.createTelreplyModel(telreplyModel);
        telreplyModel = setTelreplyModelDesc(telreplyModel);
        return telreplyModel;
    }

    public TelreplyModel saveTelreplyModel(TelreplyModel telreplyModel) {
        telreplyModel = telreplyDAO.saveTelreplyModel(telreplyModel);
        telreplyModel = setTelreplyModelDesc(telreplyModel);
        return telreplyModel;
    }

    public void deleteTelreply(Long replyId) {
        telreplyDAO.deleteTelreplyModel(replyId);
    }

    public TelreplyModel getTelreply(Long replyId) {
        TelreplyModel telreplyModel = telreplyDAO.getTelreplyModel(replyId);
        telreplyModel = setTelreplyModelDesc(telreplyModel);
        return telreplyModel;
    }

    public List searchTelreplyList(Long recordId,boolean isKeyIndicator) {
        List l = new ArrayList();
        List list = telreplyDAO.searchTelreplyList(recordId,isKeyIndicator);
        if (list != null && !list.isEmpty()) {
            Iterator itor = list.iterator();
            while (itor.hasNext()) {
                TelreplyModel telreplyModel = (TelreplyModel) itor.next();
                telreplyModel = setTelreplyModelDesc(telreplyModel);
                l.add(telreplyModel);
            }
        }
        return l;
    }

    public TelreplyModel setTelreplyModelDesc(TelreplyModel telreplyModel) {
        if (telreplyModel == null) {
            return null;
        }

        if (!StringUtil.isEmpty(telreplyModel.getReplyBy())) {
            try {
                UserModel userModel = userMgr.getUser(telreplyModel.getReplyBy());
                if (userModel != null) {
                    telreplyModel.setReplyByDesc(StringUtil.splitString(userModel.getUserName()));
                }
            } catch (Exception e) {
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            }
        }
         if (!StringUtil.isEmpty(telreplyModel.getReplyFrom())) {
              try {
                UserModel userModel1 = userMgr.getUser(telreplyModel.getReplyFrom());
                 if (userModel1 != null) {
                    telreplyModel.setReplyFromDesc(StringUtil.splitString(userModel1.getUserName()));
                }
            } catch (Exception e) {
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            }
         }

        if (!StringUtil.isEmpty(telreplyModel.getSystemUpdateStatus())) {
            telreplyModel.setSystemUpdateStatusDesc(codeLookupMgr.getCodeDesc(CodeConstant.CODE_SYSUPDATE_STATUS_LIST, telreplyModel.getSystemUpdateStatus()));
        }
        return telreplyModel;
    }
}
